{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 湿空气焓湿图查询\n",
    "\n",
    "原理：利用已有的免费在线查询网站构件 `API`。输入，输出到文件供VBA调用。\n",
    "\n",
    "在线查询网址：http://www.buildenvi.com/gongju/psychrometrics#inputform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_pvs(t):\n",
    "    '''\n",
    "    输入：干球\n",
    "    输出：饱和水蒸气分压力\n",
    "    '''\n",
    "    \n",
    "    c1 = - 5674.5359\n",
    "    c2 = 6.3925247\n",
    "    c3 = - 0.009677843\n",
    "    c4 = 6.2215701e-7\n",
    "    c5 = 2.0747825e-9\n",
    "    c6 = - 9.484024e-13\n",
    "    c7 = 4.1635019\n",
    "    c8 = - 5800.2206\n",
    "    c9 = 1.3914993\n",
    "    c10 = - 0.048640239\n",
    "    c11 = 0.000041764768\n",
    "    c12 = - 1.4452093e-8\n",
    "    c13 = 6.5459673\n",
    "    ta=t+273.15\n",
    "    \n",
    "    if t<0:\n",
    "        pvs=0.001*math.exp(c1/ta +c2 +c3*ta +c4*ta**2 + c5*ta**3 + c6*ta**4 + c7*math.log(ta))\n",
    "    else:\n",
    "        pvs = 0.001*math.exp(c8/ta + c9 +c10*ta + c11*ta**2 + c12*ta**3 + c13*math.log(ta))\n",
    "    \n",
    "    return pvs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_pv(t,tw,atm):\n",
    "    '''\n",
    "    输入：干球/湿球/大气压\n",
    "    输出：水蒸气分压力\n",
    "    '''\n",
    "    \n",
    "    pvs=cal_pvs(tw)   \n",
    "    ds=(pvs/(atm-pvs))*0.621945\n",
    "    \n",
    "    if tw<=0:\n",
    "        d = ((2830 - 0.24 * tw) * ds - 1.006 * (t - tw)) / (2830 + 1.86 * t - 2.1 * tw)\n",
    "        pv = atm * (d / (0.621945 + d))\n",
    "    else:\n",
    "        d = ((2501 - 2.326 * tw) * ds - 1.006 * (t - tw)) / (2501 + 1.86 * t - 4.186 * tw)\n",
    "        pv = atm * (d / (0.621945 + d))\n",
    "    \n",
    "    return pv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_td(pv):\n",
    "    \n",
    "    '''\n",
    "    Input: 水蒸气分压力\n",
    "    Output: 露点温度\n",
    "    '''\n",
    "    \n",
    "    y=math.log(pv)\n",
    "    td = 6.09 + 12.608 * y + 0.4959 * y**2\n",
    "    if td>=0:\n",
    "        td = 6.54 + 14.526 * y + 0.7389 * y**2 + 0.09486 * y**3 + 0.4569 * pv**0.1984\n",
    "    \n",
    "    return td"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参数名称：\n",
    "- `tw`: wet bulb\n",
    "- `rh`: relat_hum，相对湿度\n",
    "- `td`: 露点温度\n",
    "- `d`: 含湿量\n",
    "- `h`: 比焓\n",
    "- `pv`: 水蒸气分压力\n",
    "- `pvs`： 饱和水蒸气分压力"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_d(t,tw,atm):\n",
    "    \n",
    "    '''\n",
    "    Input: 干球/湿球/水蒸气分压力\n",
    "    Output：含湿量（kg/kg）\n",
    "    '''\n",
    "    \n",
    "    pv=cal_pv(t,tw,atm)\n",
    "    d = 0.621945 * pv / (atm - pv)\n",
    "    \n",
    "    return d    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_d_pv(atm,pv):\n",
    "    \n",
    "    '''\n",
    "    Input: 大气压、水蒸气分压力\n",
    "    Output：含湿量（kg/kg）\n",
    "    '''\n",
    "    \n",
    "    d_pv = 0.621945 * pv / (atm - pv)\n",
    "    return d_pv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_h(t,tw,atm):\n",
    "    \n",
    "    '''比焓'''\n",
    "    h = 1.006 * t + cal_d(t, tw, atm) * (2501 + 1.86 * t)\n",
    "    return h\n",
    "\n",
    "def cal_h_d(t, d):\n",
    "    return  1.006 * t + d * (2501 + 1.86 * t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_rh(t, tw, atm):\n",
    "    if t!=tw :\n",
    "        rh= 100 * cal_pv(t, tw, atm) / cal_pvs(t)\n",
    "    else:\n",
    "        rh=100\n",
    "    \n",
    "    return rh        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_v(t, tw, atm):\n",
    "    \n",
    "    '''比容'''\n",
    "    return 0.287042 * (t + 273.15) * (1 + 1.607858 * cal_d(t, tw, atm)) / atm\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def cal_tw(h, atm):\n",
    "    acc=0.0001\n",
    "    tlow=-100\n",
    "    thigh=h/1.006\n",
    "    \n",
    "    for j in range(1,100):\n",
    "        dt = (thigh - tlow) * 0.5\n",
    "        tw = tlow + dt\n",
    "        pvs = cal_pvs(tw)\n",
    "        ds = cal_d_pv(atm, pvs)\n",
    "        hmid = cal_h_d(tw, ds)\n",
    "    \n",
    "        if (h-hmid)>0:\n",
    "            tlow=tw\n",
    "        else:\n",
    "            thigh=tw\n",
    "        if abs(dt)<acc :\n",
    "            break\n",
    "    \n",
    "    return tw\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {
    "height": "340px",
    "left": "1px",
    "right": "1387px",
    "top": "107px",
    "width": "212px"
   },
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
